package org.hot100_3;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class leetcode139 {
    public boolean wordBreak(String s, List<String> wordDict) {

        int n = s.length();
        boolean[] dp = new boolean[n + 1];

        Arrays.fill(dp, false);
        dp[0] = true;
        HashSet<String> hashSet = new HashSet<>();

        for (String str : wordDict) {
            hashSet.add(str);
        }

        for (int i = 1; i <=n; i++) {
            for (int j = 0; j < wordDict.size(); j++) {
                int wordLength = wordDict.get(j).length();
                if (i >= wordLength) {
                    if (dp[i - wordLength] && hashSet.contains(s.substring(i - wordLength, i))) {
                        dp[i] = true;
                        break;
                    }
                }
            }
        }

        return dp[n];
    }
}
